#include "mytabwidget.h"

MyTabWidget::MyTabWidget(QWidget *parent):QTabWidget(parent)
{

}

void MyTabWidget::showFormatData(Format* format)
{
    m_format = format;
    for (auto it = format->screens.begin(); it != format->screens.end(); it++)
        tabAddScreen(it.value());

    //默认打开第一个页面
    setCurrentIndex(0);

    setAllChildrenLossSelect();
}

//将所有子控件显示为未选择状态
void MyTabWidget::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    setAllChildrenLossSelect();
}

void MyTabWidget::setAllChildrenLossSelect()
{
    for (auto it = m_screens_data.begin(); it != m_screens_data.end(); it++)
        it.value().screen_widget->setAllChildrenLossSelect();
}

void MyTabWidget::addPartition(int screen_id, Partition add_partition)
{
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->addPartition(add_partition);
}
void MyTabWidget::addLayout(int screen_id, int partition_id, Layout add_layout)
{
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->addLayout(add_layout);
}
void MyTabWidget::addItem(int screen_id, int partition_id, int layout_id, Item add_item)
{
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->getMap()[layout_id].layout_widget->addItem(add_item);
}
void MyTabWidget::addCabPos(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str, CabPosItem add_cabpositem)
{
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->getMap()[layout_id].layout_widget->addCabPos(item_id, add_cabpositem, cab_pos_item_str);
}

//删除partition
void MyTabWidget::delPartition(int screen_id, int partition_id)
{
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->delPartition(partition_id);
}
void MyTabWidget::delLayout(int screen_id, int partition_id, int layout_id)
{
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);
    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->delLayout(layout_id);
}
void MyTabWidget::delItem(int screen_id, int partition_id, int layout_id, int item_id)
{
    setCurrentChangeedScreenFromId(screen_id);
    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->getMap()[layout_id].layout_widget->delItem(item_id);
}
void MyTabWidget::delCabPos(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str)
{
    setCurrentChangeedScreenFromId(screen_id);
    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->getMap()[layout_id].layout_widget->delCabPos(item_id, cab_pos_item_str);
}



//选中partition
void MyTabWidget::setPartitionSelected(int screen_id, int partition_id)
{
    //选都不选中
    setAllChildrenLossSelect();
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->selectChild(partition_id);
}

//选中layout
void MyTabWidget::setLayoutSelected(int screen_id, int partition_id, int layout_id)
{
    //选都不选中
    setAllChildrenLossSelect();
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->setCurrentChangedLayoutFromId(layout_id);
}

//选中item
void MyTabWidget::setItemSelected(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str)
{
    //都不选中
    setAllChildrenLossSelect();
    //先翻screen页
    setCurrentChangeedScreenFromId(screen_id);

    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->setCurrentChangedLayoutFromId(layout_id);

    m_screens_data[screen_id].screen_widget->getMap()[partition_id].partition_widget->getMap()[layout_id].layout_widget->selectChild(item_id, cab_pos_item_str);
}

void MyTabWidget::tabUpdateScreens(Format* format)
{
    for (auto it = format->screens.begin(); it != format->screens.end(); it++)
    {
        m_screens_data[it.key()].screen_data = it.value();
        m_screens_data[it.key()].screen_widget->updateScreen(it.value());
    }
}
void MyTabWidget::tabAddScreen(Screen add_screen)
{
    //添加screen tab页
    ScreenShowData one_data;
    one_data.screen_widget = new ScreenWidget(add_screen);
    one_data.screen_data = add_screen;
    //第一次添加的时候会出发changed但是数据没准备好，所以提前给数据，第二次以后addTab不触发，通过setCurrentIndex才出发changed
    if (count() == 0)
        m_screens_data[0] = one_data;
    int id = add_screen.id;

    //设置拉伸
    QScrollArea *scroll_tab = new QScrollArea;
    scroll_tab->setWidget(one_data.screen_widget);

//    QWidget *parent_widget = new QWidget;
//    parent_widget->setContentsMargins(0, 0, 0, 0);

//    QHBoxLayout *layout = new QHBoxLayout;
//    layout->setContentsMargins(0, 0, 0, 0);

//    parent_widget->setLayout(layout);

//    layout->addWidget(one_data.screen_widget);
    int new_index = addTab(scroll_tab, QString("screen%1").arg(id));
    m_screens_data[id] = one_data;
    setCurrentIndex(new_index);

    //设置screen tab页的颜色大小
    one_data.screen_widget->move(0, 0);
    one_data.screen_widget->setMinimumSize(m_format->resolution.width, m_format->resolution.height);
}

//根据screen_id删除页面
void MyTabWidget::delScreen(int screen_id)
{
    for (int i = 0; i < count(); i++)
    {
        QString tab_text = tabText(i);
        if (screen_id == tab_text.right(tab_text.size() - 6).toInt())
        {
            m_screens_data.remove(screen_id);
            removeTab(i);
            return;
        }
    }
}

//根据index删除当前页
int MyTabWidget::tabDelCurrentScreen()
{
    int curr_index = currentIndex();

    //先获取tab数据再删除
    QString tab_text = tabText(curr_index);

    removeTab(curr_index);

    //通过tab内容获取id或者子窗口数据获取id
    int screen_id = tab_text.right(tab_text.size() - 6).toInt();

    m_screens_data.remove(screen_id);

    return screen_id;
}

int MyTabWidget::getCurrentChangedScreenId(int index)
{
    QString tab_text = tabText(index);
    int screen_id = tab_text.right(tab_text.size() - 6).toInt();
    if (m_screens_data.contains(screen_id))
        m_screens_data[screen_id].screen_widget->sortPartitionWidgets();
    return screen_id;
}
//screen翻页
void MyTabWidget::setCurrentChangeedScreenFromId(int screen_id)
{
    for (int i = 0; i < count(); i++)
    {
        QString tab_text = tabText(i);
        if (screen_id == tab_text.right(tab_text.size() - 6).toInt())
        {
            setCurrentIndex(i);
            m_screens_data[screen_id].screen_widget->sortPartitionWidgets();
        }
    }
}
